//
//  Kylin_TaBarViewController.m
//  01-LotteryBaseFrame
//
//  Created by Kylin on 16/1/4.
//  Copyright © 2016年 kylin. All rights reserved.
//

#import "Kylin_TaBarViewController.h"
#import "Kylin_ArenaViewController.h"
#import "Kylin_DiscoverTableViewController.h"
#import "Kylin_HallTableViewController.h"
#import "Kylin_HistoryTableViewController.h"
#import "Kylin_MyLotteryViewController.h"
#import "Kylin_tabBar.h"
#import "Kylin_NavigationViewController.h"
#import "Kylin_ArenaNavigationViewController.h"

@interface Kylin_TaBarViewController ()<Kylin_tabBarDelegate>
@property (strong,nonatomic)NSMutableArray *items;
@end

@implementation Kylin_TaBarViewController
// 赖加载没有才创建
- (NSMutableArray *)items{
    if (!_items) {
        _items = [NSMutableArray array];
    }
    return _items;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    // 一加载View自动加截所有子控件
    [self setUpAllChildViewController];
    
    // 2:自定义tarBar
    [self setUpTabBar];
    
}

// 调用系统的 [super 方法]
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    for (UIView *view in self.tabBar.subviews) {
//
        
//         一种方法 逆向思维
        if (![view isKindOfClass:[Kylin_tabBar class]]) {
            
            [view removeFromSuperview];
        }
        
        // 第二种方法, 但不能是UIView class 因自定义的tabBar也是继承UIView
//        if ([view isKindOfClass:[UIControl class]]) {
//            [view removeFromSuperview];
//        }
       
    }
}
- (void)setUpTabBar{
   
    // 移除系统tabBar
//[self.tabBar removeFromSuperview];
    
  // 2:添加自定义tabBar
    Kylin_tabBar *tabBar = [[Kylin_tabBar alloc]init];
    // 2.1 添加自定义的View到当前view上面去
    
    [self.tabBar addSubview:tabBar];
    
    // 因为当前tabBar 移出时还没有出代码块所有还可以获取frame值
    tabBar.frame = self.tabBar.bounds;
    tabBar.backgroundColor = [UIColor redColor];
    
    // 传值 把tabBarItem传给自定义的tabBar
    tabBar.items = self.items;
    
    //设置代理
    tabBar.delegate = self;
    
}

- (void)tabBar:(Kylin_tabBar *)tabBar index:(NSInteger)index{
    
  
    // 传index 是让当前tabBar切换到对应的控制器
    self.selectedIndex = index;
}




// 搭建主框架
- (void)setUpAllChildViewController{
    
    // 布局所有子控件
   
     // 1:购彩大厅
    Kylin_HallTableViewController *hall = [[Kylin_HallTableViewController alloc]init];
//    hall.view.backgroundColor = [UIColor redColor];
    
    [self setUpOneChildViewController:hall image:[UIImage imageNamed:@"TabBar_Hall_new"] selectImage:[UIImage imageNamed:@"TabBar_Hall_selected_new"]title:@"购彩大厅"];
    
    // 2:竞技场
    Kylin_ArenaViewController *arena = [[Kylin_ArenaViewController alloc]init];
//    arena.view.backgroundColor = [UIColor yellowColor];
    [self setUpOneChildViewController:arena image:[UIImage imageNamed:@"TabBar_Arena_new"] selectImage:[UIImage imageNamed:@"TabBar_Arena_selected_new"]title:nil];
    
    // 3:发现
    // 加载一个 storyboard
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Kylin_DiscoverTableViewController" bundle:nil];
    
    // 设置发现在storyboard初始化
    Kylin_DiscoverTableViewController *discover = [storyboard instantiateInitialViewController];
    
       [self setUpOneChildViewController:discover image:[UIImage imageNamed:@"TabBar_Discovery_new"]selectImage:[UIImage imageNamed:@"TabBar_Discovery_selected_new"]title:@"发现"];
    
    // 4:开奖记录
    Kylin_HistoryTableViewController *history = [[Kylin_HistoryTableViewController alloc]init];
//    history.view.backgroundColor = [UIColor blueColor];
    [self setUpOneChildViewController:history image:[UIImage imageNamed:@"TabBar_History_new"] selectImage:[UIImage imageNamed:@"TabBar_History_selected_new"]title:@"开奖记录"];
    
    //5:我的彩票
    
    Kylin_MyLotteryViewController *myLottery = [[Kylin_MyLotteryViewController alloc]init];
//    myLottery.view.backgroundColor = [UIColor cyanColor];
    [self setUpOneChildViewController:myLottery image:[UIImage imageNamed:@"TabBar_MyLottery_new"] selectImage:[UIImage imageNamed:@"TabBar_MyLottery_selected_new"]title:@"我的彩票"];
    
}

- (void)setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image selectImage:(UIImage *)selectImage title:(NSString *)title{
    
    // 创建导航控制器
    UINavigationController *nav = [[Kylin_NavigationViewController alloc]initWithRootViewController:vc];
    
  
    
    // 包装导航控制器
    // 导航控制器上面的内容由栈顶控制器navigationItem决定
    // 如果是以下控制器使用系统默认导航标识
    if ([vc isKindOfClass:[Kylin_ArenaViewController class]]) {
        nav = [[Kylin_ArenaNavigationViewController alloc]initWithRootViewController:vc];
    }
      vc.navigationItem.title = title;
    [self addChildViewController:nav];
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selectImage;
    // 保存所有tabBarItem
    [self.items addObject:vc.tabBarItem];
    
}
@end
